package com.example.boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.LogAnno;
import com.example.common.Result;
import com.example.common.enums.LogType;
import com.example.pojo.entity.Notice;
import com.example.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公告信息表前端操作接口
 **/
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    /**
     * 新增
     */
    @LogAnno(title = "公告管理", type = LogType.ADD)
//    @PreAuthorize("hasAuthority('NOTICE_ADD')")
    @PostMapping("/add")
    public Result add(@RequestBody Notice notice) {
        noticeService.add(notice);
        return Result.success();
    }

    /**
     * 删除
     */
    @LogAnno(title = "公告管理", type = LogType.DELETE)
//    @PreAuthorize("hasAuthority('NOTICE_DELETE')")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        noticeService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @LogAnno(title = "公告管理", type = LogType.DELBATCH)
//    @PreAuthorize("hasAuthority('NOTICE_DELETE')")
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        noticeService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @LogAnno(title = "公告管理", type = LogType.UPDATE)
//    @PreAuthorize("hasAuthority('NOTICE_EDIT')")
    @PutMapping("/update")
    public Result updateById(@RequestBody Notice notice) {
        noticeService.updateById(notice);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
//    @PreAuthorize("hasAuthority('NOTICE_QUERY')")
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Notice notice = noticeService.selectById(id);
        return Result.success(notice);
    }

    /**
     * 查询所有
     */
//    @PreAuthorize("hasAuthority('NOTICE_QUERY')")
    @GetMapping("/selectAll")
    public Result selectAll() {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted",0);
        List<Notice> list = noticeService.list(queryWrapper);


        return Result.success(list);
    }

    /**
     * 分页查询
     */
//    @PreAuthorize("hasAuthority('NOTICE_PAGEQUERY')")
    @GetMapping("/selectPage")
    public Result selectPage( String title,
                              Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Notice> page = noticeService.selectPage(title, pageNum, pageSize);
        return Result.success(page);
    }

}
